package com.mcjtf.prefix.commands;

import com.mcjtf.prefix.PrefixManager;
import com.mcjtf.prefix.api.event.PrefixChangeEvent;
import com.mcjtf.prefix.model.User;
import com.mcjtf.prefix.service.ConfigService;
import com.mcjtf.prefix.service.PrefixService;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class AdminCommand {
    private final PrefixManager main;
    private final PrefixService prefixService;

    public AdminCommand() {
        main = PrefixManager.getInstance();
        prefixService = main.getPrefixService();
    }

    @SubCommand(cmd = "reload", desc = "重载配置文件", permission = "prefixmanager.reload", sender = SubCommand.SenderType.ALL)
    public void reload(CommandSender sender, String[] args) {
        prefixService.save();
        main.reloadConfig();
        main.format.clear();
        prefixService.getUserMap().clear();
        main.groups.clear();
        main.temp.clear();
        main.getConfigService().reload();
        for (Player ps : main.getServer().getOnlinePlayers()) {
            main.getConfigService().loadPlayer(ps);
        }
        sender.sendMessage(ConfigService.PREFIX + ChatColor.GREEN + "插件已重载.");
    }

    @SubCommand(cmd = "add", usage = "[玩家ID] [prefix/suffix] [称号]", desc = "为玩家添加称号", permission = "prefixmanager.add", min = 4, max = 4, sender = SubCommand.SenderType.ALL)
    public void add(CommandSender sender, String[] args) {
        if (!argsMatch(args[2])) {
            sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "参数错误.");
            return;
        }
        String data = args[3];
        if (!prefixService.loadPlayer(args[1])) {
            sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "玩家不存在或不在线.");
            return;
        }
        User user = main.getPrefixService().getUser(args[1]);
        if (args[2].equalsIgnoreCase("prefix") || args[2].equalsIgnoreCase("p")) {
            if (user.getPrefixes().contains(data)) {
                sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "当前玩家已拥有此前缀,请勿重复添加");
                return;
            }
            user.addPrefix(data);
            prefixService.addPrefix(user.getName(), user.getPrefixes());
            sender.sendMessage(ConfigService.PREFIX + main.msg("&a已成功为玩家&6" + args[1] + "&a添加前缀:" + data));
        } else {
            if (user.getSuffixes().contains(data)) {
                sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "当前玩家已拥有此后缀,请勿重复添加");
                return;
            }
            user.addSuffix(data);
            prefixService.addSuffix(user.getName(), user.getSuffixes());
            sender.sendMessage(ConfigService.PREFIX + main.msg("&a已成功为玩家&6") + args[1] + "&a添加后缀:" + data);
        }
    }

    @SubCommand(cmd = "del", usage = "[玩家ID] [prefix/suffix] [称号序号]", desc = "删除玩家指定前缀/后缀", permission = "prefixmanager.del", min = 4, max = 4, sender = SubCommand.SenderType.ALL)
    public void del(CommandSender sender, String[] args) {
        if (!argsMatch(args[2]) || !main.isInt(args[3])) {
            sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "参数错误.");
            return;
        }
        if (!prefixService.loadPlayer(args[1])) {
            sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "玩家不存在或不在线.");
            return;
        }
        User user = prefixService.getUser(args[1]);
        if (!prefixService.del(user, args[2], Integer.parseInt(args[3]))) {
            sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "删除失败,无效的称号序号.");
            return;
        }

        PrefixChangeEvent call = new PrefixChangeEvent(main.getServer().getPlayer(user.getName()), args[2], "");
        main.getServer().getPluginManager().callEvent(call);

        sender.sendMessage(ConfigService.PREFIX + ChatColor.GREEN + "已成功删除指定称号.");
    }

    @SubCommand(cmd = "show", usage = "[玩家ID] [prefix/suffix]", desc = "显示指定玩家的前缀/后缀", permission = "prefixmanager.show", min = 3, max = 3, sender = SubCommand.SenderType.ALL)
    public void show(CommandSender sender, String[] args) {
        if (!argsMatch(args[2])) {
            sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "参数错误.");
            return;
        }
        if (!prefixService.loadPlayer(args[1])) {
            sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "玩家不存在或不在线.");
            return;
        }
        User user = prefixService.getUser(args[1]);
        prefixService.list(sender, user, args[2]);
    }

    @SubCommand(cmd = "save", desc = "保存玩家数据", permission = "prefixmanager.save", sender = SubCommand.SenderType.ALL)
    public void save(CommandSender sender, String[] args) {
        prefixService.save();
        sender.sendMessage(ConfigService.PREFIX + ChatColor.GREEN + "玩家数据,配置文件已保存.");
    }

    private boolean argsMatch(String arg) {
        if (arg.equalsIgnoreCase("prefix") || arg.equalsIgnoreCase("p")) {
            return true;
        }
        return arg.equalsIgnoreCase("suffix") || arg.equalsIgnoreCase("s");
    }
}
